package cn.org.exam.service.impl;

import cn.org.exam.mapper.UserMapper;
import cn.org.exam.model.entity.Role;
import cn.org.exam.model.entity.User;
import cn.org.exam.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User findByUsername(String username) {
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            List<Role> roles = userMapper.listRolesByUserId(user.getId());
            user.setRoles(roles);
        }
        return user;
    }


    @Override
    public User findById(Long id) {
        User user = userMapper.selectById(id);
        if (user != null) {
            List<Role> roles = userMapper.listRolesByUserId(user.getId());
            user.setRoles(roles);
        }
        return user;
    }


}